package uk.org.landeg.euler.problems;

import java.util.ArrayList;

import uk.org.landeg.euler.problems.framework.AbstractProblem;
import uk.org.landeg.euler.problems.framework.ProblemLauncher;
import uk.org.landeg.euler.problems.lib.PrimeLib;

public class Problem027 extends AbstractProblem{
	ArrayList<Long> primes = PrimeLib.seqPrimes(10000L);
	public static void main(String[] args) {
		ProblemLauncher launcher = new ProblemLauncher(new Problem027());
		launcher.solve();		
	}

	@Override
	public String getDescription() {
		// TODO Auto-generated method stub
		return "Find the product of the coefficients, a and b, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n = 0.";
	}

	@Override
	public Integer getProblemNumber() {
		// TODO Auto-generated method stub
		return 27;
	}

	private int countPrimesFromQuad (int a, int b) {
		int n = 0;
		int count = 0;
		while (primes.contains((long)(n*n+a*n+b))) {
			count++;n++;
		}
		return count;
	}
	
	@Override
	public Object solve() {
		int maxIt = 0, it;
		int maxA = 0, maxB = 0;
		for (long b : primes) {
			if (b >= 1000) {
				break;
			}
			for (int a = -1000 ; a < 1000 ; a++) {
				it = countPrimesFromQuad(a, (int)b);
				if (it > maxIt) {
					maxA = a;
					maxB = (int)b;
					maxIt = it;
				}
			}
		}
		
		return "max primes generated : " + maxIt + String.format(" at a=%d b=%d ab=%d", maxA, maxB, maxA*maxB);
	}
}
